// echo aux function only, should not compile alone.

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;
using HarmonyLib;
namespace GongfaEffect;
//! 目前为止，我们知道，福利姬的素材id越接近1（眉毛是2），福利姬的魅力就越高。
//! 因此，我们只需要把福利姬的素材id往1这个方向靠拢就好。
public static partial class Utils {
    static HashSet<short> hairHash=new HashSet<short>(){1,2,3,21,30,40,42,47,51,55};// 避免这些前发
    public static bool beautify(this GameData.Domains.Character.Character character,GameData.Common.DataContext context){ // 函数吃一个人的引用和hairHash这个全局变量，对人物进行一次容貌修正。
        var avatar=character.GetAvatar();
        var charm1=avatar.GetBaseCharm();
        var type=avatar.GetBodyType();// (avatar.AvatarId-1)/2
        var op=-1;
//        var nm=character.GetFullName().GetName(character.GetGender(),GameData.Domains.DomainManager.World.GetCustomTexts()); // debug
        if(type!=0){ // 对体型的美容，这样会优先从大体型变成小体型
            avatar.AvatarId-=2;
            goto https;
        }
        var counter=0;
        if(avatar.EyesMainId>1 ){
            int count=avatar.EyesMainId-1;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count)op=0;
        }
        if(avatar.EyebrowId!=1 ){
            if(avatar.EyebrowId<1){
                avatar.EyebrowId=1;
                goto https;
            } else {
                int count=avatar.EyebrowId-1;
                if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count)op=1;
            }
        }
        if(avatar.NoseId>1 ){
            int count=avatar.NoseId-1;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count)op=2;
        }
        if(avatar.MouthId>1 ){
            int count=avatar.MouthId-1;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count)op=3;
        }
        if(avatar.Feature1Id!=2 ){
            if(avatar.Feature1Id==1){
                avatar.Feature1Id=20;
                goto https;
            }else if(avatar.Feature1Id>2) {
                int count=avatar.Feature1Id-1;
                if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count)op=4;
            }
        }
        if(hairHash.Contains(avatar.FrontHairId) ){
            avatar.FrontHairId=20;
            goto https;
        }
        if(avatar.Feature2Id>1){
            int count=avatar.Feature2Id-1;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count)op=5;
        }
        if(avatar.Beard1Id>1){
            int count=avatar.Beard1Id-1;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count)op=6;
        }
        if(avatar.Beard2Id>1){
            int count=avatar.Beard2Id-1;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count)op=7;
        }
        if(avatar.Wrinkle1Id>1){
            int count=avatar.Wrinkle1Id-1;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count)op=8;
        }
        if(avatar.Wrinkle2Id>1){
            int count=avatar.Wrinkle2Id-1;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count)op=9;
        }
        if(avatar.Wrinkle3Id>1){
            int count=avatar.Wrinkle3Id-1;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count)op=10;
        }
        int diff=0;
        if(avatar.NoseHeightPercent!=30){
            int count=(Math.Abs(avatar.NoseHeightPercent-30)+9)/10;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count){
                op=11;
                if(count<=1){diff=avatar.NoseHeightPercent-30;}else{diff=10*Math.Sign(avatar.NoseHeightPercent-30);}
            }
        }
        if(avatar.EyesDistancePercent!=50){
            int count=(Math.Abs(avatar.EyesDistancePercent-50)+9)/10;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count){
                op=12;
                if(count<=1){diff=avatar.EyesDistancePercent-50;}else{diff=10*Math.Sign(avatar.EyesDistancePercent-50);}
            }
        }
        if(avatar.EyebrowDistancePercent!=50){
            int count=(Math.Abs(avatar.EyebrowDistancePercent-50)+9)/10;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count){
                op=13;
                if(count<=1){diff=avatar.EyebrowDistancePercent-50;}else{diff=10*Math.Sign(avatar.EyebrowDistancePercent-50);}
            }
        }
        if(avatar.MouthHeightPercent!=50){
            int count=(Math.Abs(avatar.MouthHeightPercent-50)+9)/10;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count){
                op=14;
                if(count<=1){diff=avatar.MouthHeightPercent-50;}else{diff=10*Math.Sign(avatar.MouthHeightPercent-50);}
            }
        }
        if(avatar.EyesHeightPercent!=50){
            int count=(Math.Abs(avatar.EyesHeightPercent-50)+9)/10;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count){
                op=15;
                if(count<=1){diff=avatar.EyesHeightPercent-50;}else{diff=10*Math.Sign(avatar.EyesHeightPercent-50);}
            }
        }
        if(avatar.EyesAngle!=200){
            int count=(Math.Abs(avatar.EyesAngle-200)+39)/40;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count){
                op=16;
                if(count<=1){diff=avatar.EyesAngle-200;}else{diff=40*Math.Sign(avatar.EyesAngle-200);}
            }
        }
        if(avatar.EyebrowAngle!=250){
            int count=(Math.Abs(avatar.EyebrowAngle-250)+39)/40;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count){
                op=17;
                if(count<=1){diff=avatar.EyebrowAngle-250;}else{diff=40*Math.Sign(avatar.EyebrowAngle-250);}
            }
        }
        if(avatar.EyebrowHeight!=330){
            int count=(Math.Abs(avatar.EyebrowHeight-330)+49)/50;
            if(count>0){counter+=count;}if(count>0 && context.Random.Next(counter)<count){
                op=18;
                if(count<=1){diff=avatar.EyebrowHeight-330;}else{diff=50*Math.Sign(avatar.EyebrowHeight-330);}
            }
        }
        switch(op){
        case -1:return false;
        case 0:
            avatar.EyesMainId--;
            var eye=GameData.Domains.Character.AvatarSystem.AvatarManager.Instance.GetAvatarGroup(avatar.AvatarId).EyesGroup.Find((GameData.Domains.Character.AvatarSystem.AvatarRes.EyeRes e)=>e.Id==avatar.EyesMainId);
            if(eye!=null){
                avatar.EyesLeftId=eye.LeftEye.SubId;
                avatar.EyesRightId=eye.RightEye.SubId;
            } else {
                avatar.EyesLeftId=0;
                avatar.EyesRightId=0;
            }
            goto https;
        case 1:avatar.EyebrowId--;goto https;
        case 2:avatar.NoseId--;goto https;
        case 3:avatar.MouthId--;goto https;
        case 4:avatar.Feature1Id--;goto https;
        case 5:avatar.Feature2Id--;goto https;
        case 6:avatar.Beard1Id--;goto https;
        case 7:avatar.Beard2Id--;goto https;
        case 8:avatar.Wrinkle1Id--;goto https;
        case 9:avatar.Wrinkle2Id--;goto https;
        case 10:avatar.Wrinkle3Id--;goto https;
        case 11:avatar.NoseHeightPercent-=(short)diff;goto https;
        case 12:avatar.EyesDistancePercent-=(short)diff;goto https;
        case 13:avatar.EyebrowDistancePercent-=(short)diff;goto https;
        case 14:avatar.MouthHeightPercent-=(short)diff;goto https;
        case 15:avatar.EyesHeightPercent-=(short)diff;goto https;
        case 16:avatar.EyesAngle-=(short)diff;goto https;
        case 17:avatar.EyebrowAngle-=(short)diff;goto https;
        case 18:avatar.EyebrowHeight-=(short)diff;goto https;
        default:return false;
        }
//! 据说这才是Label的正确写法
//! 如果需要跳转到两个地方就再加一个http
https://gitee.com/Neutron3529/MiChangSheng_Mod/releases/tag/%E5%A4%AA%E5%90%BE%EF%BC%81
//        character.SetAvatar(avatar,context);
//        var y=character.GetBaseMainAttributes();
//        try{
//            GameData.Utilities.AdaptableLog.Info(nm.Item1+nm.Item2+"("+character.GetId().ToString()+") op"+op+"charm"+charm1+"->"+avatar.GetBaseCharm());
//        } catch(ArgumentOutOfRangeException e) {
//            GameData.Utilities.AdaptableLog.Info(nm.Item1+nm.Item2+"("+character.GetId().ToString()+") op"+op+"charm"+charm1+"-> error,NoseId="+avatar.NoseId);
//            throw(e);
//        }
        return true;
    }
}
